import http from './http'

export default {
  getGroup (id) {
    return http.get(`api/group/${id}?_format=json`)
  },

  postGroup (data) {
    return http.post(`api/group`, data)
  },

  patchGroup ({id, ...data}) {
    return http.patch(`api/group/${id}`, data)
  },

  getGroupCategory (gid, category) {
    return http.get(`api/group/${gid}/question_category?_format=json`).then(res => res.data)
  },

  postGroupCategory (gid, name) {
    return http.post(`api/group/${gid}/question_category`, { name })
  },

  patchGroupCategory (gid, tid, name) {
    return http.patch(`api/group/${gid}/question_category/${tid}`, { name })
  },

  deleteGroupCategory (gid, tid) {
    return http.delete(`api/group/${gid}/question_category/${tid}`)
  },

  getGroupMemberList (id) {
    let timestamp = new Date().getTime()
    return http.get(`api/group/${id}/member?_format=json&timestamp=${timestamp}`)
  },

  getGroupMember (gid, id) {
    return http.get(`api/group/${gid}/member/${id}?_format=json`)
  },

  addMembership (gid, uid) {
    return http.post(`api/group/${gid}/member?_format=json`, { user_id: uid })
  },

  patchMembership ({gid, id, ...data}) {
    return http.patch(`api/group/${gid}/member/${id}`, data)
  },

  removeMembership (gid, uid) {
    return http.delete(`api/group/${gid}/member/${uid}`)
  }
}
